var columnNumber = 3;

//页面的onload事件
function loadNews(){
	findNewsByID(this.opener.newsID);
}

//根据newsID查到对应的新闻记录
function findNewsByID(newsID){
	if(bXmlHttpSupport) {
    	var sUrl = 'findNewsByID.action';
    	var params = "newsID=" + newsID;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
        	if(request.readyState == 4) {
            	var oRequest = eval('(' + request.responseText + ')');
            	//alert(request.responseText);
            	var news = oRequest.news;
            	putNewsIntoPage(news);
			}
    	};
    	request.open('POST', sUrl);
    	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    	request.send(params);
    }
}

function putNewsIntoPage(news){
	var newsTitle = document.getElementById("newsTitle");
	newsTitle.value = news.title;
	var newsAuthor = document.getElementById("newsAuthor");
	newsAuthor.value = news.author;
	var newsType = document.getElementById("newsType");
	newsType.value = news.type;
	var imgLen = news.newsImgs.length;
	if (imgLen == 0){
		return;
	}
	var rowObj;
	var tbCurrentImgs = document.getElementById("tbCurrentImgs");
	//计算要生成的行数
	var lines = Math.ceil(imgLen / columnNumber);
	for ( var i = 0; i < lines; i++){
		rowObj = document.createElement("tr");
		for (var j = i * columnNumber; j < (i + 1) * columnNumber; j++){
			var html;
			if ( j > imgLen -1){
				html = "没有图片";
			}else{
				html = 	"<div id='tdDIV'>" +
						"<div id='tdDIV1'><img src='loadImgByID.action?imgID=" + news.newsImgs[j].newsImgID+"'/></div>" +
						"<div id='tdDIV2'><input type='button' id='delImg' value='删除' onclick='delExistedImg(" + news.newsImgs[j].newsImgID+",this);'/></div>" +
						"</div>";
			}
			addOneTD(rowObj, html, "");
			tbCurrentImgs.appendChild(rowObj);
		}
	}
}

//点击删除按钮后，将该位置换回添加图片按钮，并访问后台，将imgID加入待删除列表
function delExistedImg(imgID, delBtn){
	if(bXmlHttpSupport) {
    	var sUrl = 'delOneImg.action';
    	var params = "imgID=" + imgID;
        var request = new XMLHttpRequest();
        request.onreadystatechange = function() {
        	if(request.readyState == 4) {
        		var currentDIV = delBtn.parentNode.parentNode;
        		var html = 	"没有图片";
        		currentDIV.innerHTML = html;
			}
    	};
    	request.open('POST', sUrl);
    	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
    	request.send(params);
    }
}

/*=================================与后台交互============================================================*/
//提交新闻修改
function modifyNews(){
	//检查新闻类型是否已经选择了
	var newsType = document.getElementById("newsType");
	if(!selectValid(newsType)){
		alert("请选择一个新闻类型！");
		return;
	}
	//检查新闻标题是否为空
	var newsTitle = document.getElementById("newsTitle");
	if ( newsTitle.value == ""){
		alert("新闻标题不能为空！");
		return;
	}
	//检查新闻标题长度是否符合标准
	if (stringExceedLengthLimit(newsTitle.value, 50)){
		alert("新闻标题不能超过50个汉字！");
		return;
	}
	//检查新闻作者是否为空
	var newsAuthor = document.getElementById("newsAuthor");
	if (newsAuthor.value == ""){
		alert("新闻作者不能为空！");
		return;
	}
	//检查新闻作者长度是否符合标准
	if (stringExceedLengthLimit(newsAuthor.value, 25)){
		alert("新闻作者不能超过25个汉字！");
		return;
	}
	if (!validateImgs()){
		alert("如果您点击了”添加图片“，就要选择图片，否则请点击”删除“！");
		return;
	}
	
	var frmEditNews = document.getElementById("frmEditNews");
	frmEditNews.submit();
}

